package com.springboot.redis.stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Slf4j
@Service
public class PublisherService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private StreamOperations<String, String, Object> stream;

    @PostConstruct
    public void init(){
        stream = this.stringRedisTemplate.opsForStream();
    }

    public void send(String topic, String key, String value){

        log.info("开始发送消息 topic:{}, key:{}, value:{}", topic, key, value);

        HashMap<String, Object> record = new HashMap<>();

        record.put(key, value);

        stream.add(topic, record);

        log.info("topic:{} 发送消息成功！", key);

    }

}
